home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / CYCLOID.FRM < prev    next >
Text File  |  1996-03-28  |  6KB  |  221 lines

  1. VERSION 4.00
  2. Begin VB.Form CycloidForm 
  3.    Caption         =   "Cycloid"
  4.    ClientHeight    =   5310
  5.    ClientLeft      =   2085
  6.    ClientTop       =   900
  7.    ClientWidth     =   4830
  8.    Height          =   6000
  9.    Left            =   2025
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   354
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   270
  15.    Width           =   4950
  16.    Begin VB.TextBox RText 
  17.       Height          =   285
  18.       Left            =   3285
  19.       TabIndex        =   8
  20.       Text            =   "15"
  21.       Top             =   45
  22.       Width           =   615
  23.    End
  24.    Begin VB.TextBox PText 
  25.       Height          =   285
  26.       Left            =   1320
  27.       TabIndex        =   5
  28.       Text            =   "20"
  29.       Top             =   45
  30.       Width           =   615
  31.    End
  32.    Begin VB.TextBox QText 
  33.       Height          =   285
  34.       Left            =   2280
  35.       TabIndex        =   4
  36.       Text            =   "7"
  37.       Top             =   45
  38.       Width           =   615
  39.    End
  40.    Begin VB.TextBox DtText 
  41.       Height          =   285
  42.       Left            =   240
  43.       TabIndex        =   3
  44.       Text            =   "0.025"
  45.       Top             =   45
  46.       Width           =   615
  47.    End
  48.    Begin VB.CommandButton CmdGo 
  49.       Caption         =   "Go"
  50.       Default         =   -1  'True
  51.       Height          =   375
  52.       Left            =   4200
  53.       TabIndex        =   1
  54.       Top             =   0
  55.       Width           =   615
  56.    End
  57.    Begin VB.PictureBox Canvas 
  58.       AutoRedraw      =   -1  'True
  59.       Height          =   4815
  60.       Left            =   0
  61.       ScaleHeight     =   -2.2
  62.       ScaleLeft       =   -1.1
  63.       ScaleMode       =   0  'User
  64.       ScaleTop        =   1.1
  65.       ScaleWidth      =   2.2
  66.       TabIndex        =   0
  67.       Top             =   480
  68.       Width           =   4815
  69.    End
  70.    Begin VB.Label Label1 
  71.       Caption         =   "R"
  72.       Height          =   255
  73.       Index           =   0
  74.       Left            =   3120
  75.       TabIndex        =   9
  76.       Top             =   60
  77.       Width           =   255
  78.    End
  79.    Begin VB.Label Label1 
  80.       Caption         =   "P"
  81.       Height          =   255
  82.       Index           =   3
  83.       Left            =   1200
  84.       TabIndex        =   7
  85.       Top             =   60
  86.       Width           =   255
  87.    End
  88.    Begin VB.Label Label1 
  89.       Caption         =   "Q"
  90.       Height          =   255
  91.       Index           =   2
  92.       Left            =   2115
  93.       TabIndex        =   6
  94.       Top             =   60
  95.       Width           =   255
  96.    End
  97.    Begin VB.Label Label1 
  98.       Caption         =   "dt"
  99.       Height          =   255
  100.       Index           =   1
  101.       Left            =   0
  102.       TabIndex        =   2
  103.       Top             =   60
  104.       Width           =   255
  105.    End
  106.    Begin VB.Menu mnuFile 
  107.       Caption         =   "&File"
  108.       Begin VB.Menu mnuFileExit 
  109.          Caption         =   "E&xit"
  110.       End
  111.    End
  112. End
  113. Attribute VB_Name = "CycloidForm"
  114. Attribute VB_Creatable = False
  115. Attribute VB_Exposed = False
  116. Option Explicit
  117.  
  118. Const PI = 3.14159
  119. Const TWO_PI = 2 * PI
  120.  
  121. Dim P As Integer
  122. Dim Q As Integer
  123. Dim R As Integer
  124. Dim P_Q As Single
  125. Dim PQ As Integer
  126. Dim PQR As Integer
  127.  
  128. ' ************************************************
  129. ' Draw the curve on the indicated picture box.
  130. ' ************************************************
  131. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, Dt As Single)
  132. Dim t As Single
  133.  
  134.     pic.Cls
  135.     pic.CurrentX = X(start_t)
  136.     pic.CurrentY = Y(start_t)
  137.     
  138.     t = start_t + Dt
  139.     Do While t < stop_t
  140.         pic.Line -(X(t), Y(t))
  141.         t = t + Dt
  142.     Loop
  143.     
  144.     pic.Line -(X(stop_t), Y(stop_t))
  145. End Sub
  146.  
  147.  
  148.  
  149. ' ************************************************
  150. ' Non-recursively compute the greatest common
  151. ' divisor of to integers.
  152. ' ************************************************
  153. Private Function GCD(ByVal a As Integer, ByVal b As Integer) As Integer
  154. Dim B_Mod_A As Integer
  155.  
  156.     B_Mod_A = b Mod a
  157.     Do While B_Mod_A <> 0
  158.         ' Prepare the arguments for the "recursion."
  159.         b = a
  160.         a = B_Mod_A
  161.         B_Mod_A = b Mod a
  162.     Loop
  163.  
  164.     GCD = a
  165. End Function
  166.  
  167. ' ************************************************
  168. ' Calculate the values t must cross to draw a
  169. ' cycloid.
  170. ' ************************************************
  171. Sub SetTBounds(tmin As Single, tmax As Single)
  172.     tmin = 0
  173.     ' LCM / P * 2 * PI.
  174.     tmax = Q / GCD(P, Q) * TWO_PI
  175. End Sub
  176.  
  177. ' ************************************************
  178. ' Find the least common multiple of two integers.
  179. ' ************************************************
  180. Function LCM(a As Integer, b As Integer) As Integer
  181.     LCM = a * b / GCD(a, b)
  182. End Function
  183.  
  184. ' ************************************************
  185. ' The parametric function Y(t).
  186. ' ************************************************
  187. Function Y(t As Single) As Single
  188.     Y = (PQ * Sin(t) + R * Sin(t * P_Q)) / PQR
  189. End Function
  190.  
  191. ' ************************************************
  192. ' The parametric function X(t).
  193. ' ************************************************
  194. Function X(t As Single) As Single
  195.     X = (PQ * Cos(t) + R * Cos(t * P_Q)) / (PQR)
  196. End Function
  197.  
  198. Private Sub CmdGo_Click()
  199. Dim tmin As Single
  200. Dim tmax As Single
  201. Dim Dt As Single
  202.  
  203.     P = CInt(PText.Text)
  204.     Q = CInt(QText.Text)
  205.     R = CInt(RText.Text)
  206.     P_Q = P / Q
  207.     PQ = P + Q
  208.     PQR = PQ + R
  209.     
  210.     SetTBounds tmin, tmax
  211.     
  212.     Dt = CSng(DtText.Text)
  213.     DrawCurve Canvas, tmin, tmax, Dt
  214. End Sub
  215.  
  216. Private Sub mnuFileExit_Click()
  217.     Unload Me
  218. End Sub
  219.  
  220.  
  221.